package com.yidu.ladder.transfer.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yidu.ladder.transfer.dao.CloseWrapDao;
import com.yidu.ladder.transfer.domain.CloseWraps;
import com.yidu.ladder.transfer.domain.CloseWrapsVo;
import com.yidu.ladder.transfer.service.CloseWrapService;
import com.yidu.ladder.util.ExcelUtils;
/**
 * 合包业务逻辑实现类
 * @author bamboo
 *
 */
@Service
public class CloseWrapServiceImpl implements CloseWrapService{
	@Autowired
	private CloseWrapDao closeWrapsDao;
	
	
	public CloseWrapDao getCloseWrapsDao() {
		return closeWrapsDao;
	}

	public void setCloseWrapsDao(CloseWrapDao closeWrapsDao) {
		this.closeWrapsDao = closeWrapsDao;
	}

	@Override
	public Map<String, Object> selectAll(CloseWrapsVo closeWrapsVo) {
		//设置页面
		closeWrapsVo.setPage((closeWrapsVo.getPage() - 1) * closeWrapsVo.getLimit());
		//调用Dao层方法分页查询
		List<CloseWraps> list = closeWrapsDao.queryWrapWithPage(closeWrapsVo);
		
		//每次查询得到相应的记录数
		int count = closeWrapsDao.countWrap(closeWrapsVo);
		//创建一个Map接收数据返回给前端
		Map<String, Object> map = new HashMap<String, Object>();
		//map存放code
		map.put("code", 0);
		//map存放count
		map.put("count", count);
		//map存放数据
		map.put("data",list);
		//返回一个map
		return map;
	}

	/*
     * 文件导入（批量导入）
     * */
    @Override
    public String ajaxUploadExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        /*MultipartFile file = multipartRequest.getFile("file");*/

     //   System.out.println("得到数据文件");
        if(file.isEmpty()){
            try {
                throw new Exception("文件不存在！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        InputStream in =null;
        try {
            in = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
     //   System.out.println("加载流");
        List<List<Object>> listob = null;
        try {
            listob = new ExcelUtils().getBankListByExcel(in,file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        for (int i = 0; i < listob.size(); i++) {
        //    System.out.println("listob,zize是："+listob.size());
            List<Object> lo = listob.get(i);
        //    System.out.println("遍历" + listob.get(i));
            CloseWraps vo = new CloseWraps();
       //     Student j = null;

       //     System.out.println("lo.size是："+lo.size());

            if(lo.size() == 5){    //无ID这一列
           //     System.out.println("无ID");
				/*
				 * vo.setStudentName(String.valueOf(lo.get(0)));
				 * vo.setStudentNumber(String.valueOf(lo.get(1)));
				 * vo.setClassroom(String.valueOf(lo.get(2)));
				 * vo.setPosition(String.valueOf(lo.get(3)));
				 * vo.setGradeId(Integer.valueOf(String.valueOf(lo.get(4))));
				 */
            }else if(lo.size() == 6){   //有ID这一列
          //      System.out.println("有ID");
				/*
				 * vo.setStudentName(String.valueOf(lo.get(1)));
				 * vo.setStudentNumber(String.valueOf(lo.get(2)));
				 * vo.setClassroom(String.valueOf(lo.get(3)));
				 * vo.setPosition(String.valueOf(lo.get(4)));
				 * vo.setGradeId(Integer.valueOf(String.valueOf(lo.get(5))));
				 */
            }

          //  System.out.println("vo是: "+vo.toString());

            //studentMapper.addStudent(vo);

        }
        return "success";
    }


	


	
}
